#include "BlockQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <ctime>
#include <unistd.h>

void *Consumer(void *args)
{
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(args);
    while (true)
    {
        // sleep(2);
        //  1.获取数据/任务
        // Task t;
        task_t t;
        bq->Pop(&t);
        // 2.处理数据
        t();
        // cout << "Consumer -> " <<  << endl;
    }
}

void *Producer(void *args)
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(args);
    while (true)
    {
        sleep(2);
        // 1.构建数据/任务
        // int x = rand() % 10 + 1; //[1,10]
        // usleep(x*1000);
        // int y=rand()%10+1;
        // Task t(x,y);
        // 2.生产数据
        bq->Equeue(Download);
        // cout << "Productor -> " << Download() << endl;
        cout << "Productor -> Download " << endl;
    }
}

int main()
{
    BlockQueue<task_t> *bq = new BlockQueue<task_t>();
    pthread_t p1,p2,p3, c1,c2;
    pthread_create(&p1, nullptr, Producer, bq);
    pthread_create(&p2, nullptr, Producer, bq);
    pthread_create(&p3, nullptr, Producer, bq);
    pthread_create(&c1, nullptr, Consumer, bq);
    pthread_create(&c2, nullptr, Consumer, bq);

    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(p3, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
}